package cn.ekuma.epos.analysis.front.product;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.event.EventListenerList;

import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;

import com.openbravo.bean.Location;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.erp.viewbean.FrontProductAnalysisInfo;
import com.openbravo.bean.erp.viewbean.OrderLine_V;
import com.openbravo.bean.erp.viewbean.ProductAnalysis;
import com.openbravo.bean.erp.viewbean.ProductTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.panels.event.LocationChangedListener;
import com.openbravo.pos.panels.event.ProductChangedListener;

import cn.ekuma.epos.analysis.front.product.model.ProductPosSellAnalysisTableModel;
import cn.ekuma.epos.analysis.product.model.ProductAnalysisTableModel;
import cn.ekuma.epos.businesslogic.OrderUtil;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import net.sourceforge.jtimepiece.TimeUtil;

public class AnalysisModelManager {
	
	ProductPosSellAnalysisTableModel tableModel;
    List<FrontProductAnalysisInfo> productAnalysis;
    
    String detailProductID;
    
    public static String PROPERTY_DialyProductID="detailProductID";
     /** List of listeners */
    protected EventListenerList listenerList = new EventListenerList();
   
    private I_DataLogicERP dlSales;
    private Object[] afilter;
    String selectLoction;
    Date startDate;
    Date endDate;
    

    public AnalysisModelManager(I_DataLogicERP dlSales) {
        this.dlSales=dlSales;
        tableModel=new ProductPosSellAnalysisTableModel();
    }

    public void query(Object filter) throws BasicException{
            productAnalysis = dlSales.listByFilter(FrontProductAnalysisInfo.class,filter);
            tableModel.resert(productAnalysis);
    }
    
   public void query(String location,String customer,Object filter) throws BasicException{
	   afilter=(Object[])filter;
    	productAnalysis = dlSales.listFrontProductAnalysisByCustomer(location, customer, (Date)afilter[1], (Date)afilter[3]);
    	tableModel.resert(productAnalysis);
    }
    
    
    public void addProductChangedListener(ProductChangedListener l){
        listenerList.add(ProductChangedListener.class, l);
    }

    public void removeProductChangeListener(ProductChangedListener l){
       listenerList.remove(ProductChangedListener.class, l);
   }

    public ProductChangedListener[] getProductChangedListeners() {
        return (ProductChangedListener[])listenerList.getListeners(ProductChangedListener.class);
    }

    public void fireProductChanged(String id,Object o) {
	// Guaranteed to return a non-null array
	Object[] listeners = listenerList.getListenerList();
	// Process the listeners last to first, notifying
	// those that are interested in this event
	for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]==ProductChangedListener.class) {
		((ProductChangedListener)listeners[i+1]).baseBeanChanged(KeyBuilder.getKey(id),null);
	}
      }
    }

    public void addPropertyChangeListener(PropertyChangeListener l){
        listenerList.add(PropertyChangeListener.class, l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l){
        listenerList.remove(PropertyChangeListener.class, l);
    }

    public void fireDialyProductPropertyChanged(String name,String id,String productName) {
	// Guaranteed to return a non-null array
	Object[] listeners = listenerList.getListenerList();
	// Process the listeners last to first, notifying
	// those that are interested in this event
	for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]==PropertyChangeListener.class) {
		((PropertyChangeListener)listeners[i+1]).propertyChange(new PropertyChangeEvent(this,PROPERTY_DialyProductID,detailProductID,new String[]{id,productName}));
	}
      }
      detailProductID=id;
    }

    public I_DataLogicSales getDlSales() {
        return dlSales;
    }
    
    public void addLocationChangedListener(LocationChangedListener l){
		listenerList.add(LocationChangedListener.class, l);
	}
	
	public void removeLocationChangedListener(LocationChangedListener l){
		listenerList.remove(LocationChangedListener.class, l);
	}
	
	public void fireLocationChanged(Location l){
		for(LocationChangedListener list:listenerList.getListeners(LocationChangedListener.class)){
			list.valueSelectChanged(l);
		}
	}

	public void query(String location, Date startDate, Date endDate, Object[] beforeFilterParas) throws BasicException{
		afilter=(Object[])beforeFilterParas;
    	productAnalysis = dlSales.listFrontProductAnalysisByNewProducts(location, startDate,endDate, (Date)afilter[1], (Date)afilter[3]);
    	tableModel.resert(productAnalysis);
		
	}

	public String getSelectLoction() {
		return selectLoction;
	}

	public void setSelectLoction(String selectLoction) {
		this.selectLoction = selectLoction;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	

}
